package com.example.smart.adapter.home

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.lib.extend.setOnDoubleClickListener
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.databinding.ItemReservationBinding

class ReservationAdapter(context: Context) : BaseRecyclerAdapter<ItemReservationBinding, String>(context,
    mutableListOf("10:00", "11:00", "14:00", "15:00", "16:00", "17:00")) {
    var nowChoose = -1
    var lastChoose = -1
    private val textChooseColor by lazy {
        context.getColor(com.example.lib.R.color.white)
    }

    private val textColor by lazy {
        context.getColor(com.example.lib.R.color.black_a1a1ae)
    }

    private val textChooseBack by lazy {
        com.example.lib.R.drawable.shape_25_326afd_solid
    }

    private val textBack by lazy {
        com.example.lib.R.drawable.shape_25_d6e1ff_solid
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemReservationBinding> {
        return BaseViewHolder(ItemReservationBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_reservation, parent, false)))
    }

    override fun onBindView(itemView: ItemReservationBinding, bean: String, position: Int) {
        itemView.root.text = bean

        if (nowChoose == position) {
            itemView.root.setBackgroundResource(textChooseBack)
            itemView.root.setTextColor(textChooseColor)
        } else {
            itemView.root.setBackgroundResource(textBack)
            itemView.root.setTextColor(textColor)
        }

        itemView.root.setOnDoubleClickListener {
            nowChoose = position
            if (lastChoose != -1) {
                updateItem(lastChoose)
            }
            updateItem(nowChoose)
            lastChoose = position
        }
    }
}